var setting = {
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			edit: {
				enable: true,
				editNameSelectAll: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeDrag: beforeDrag,
				beforeEditName: beforeEditName,
				beforeRemove: beforeRemove,
				beforeRename: beforeRename,
				onRemove: onRemove,
				onRename: onRename,
				onClick: onClick
			}
		};

		var zNodes =[
			{id: '11',pId: '1',name:'11'},
			{id: '1',pId: '0',name:'1'},
			{id: '2',pId: '0',name:'2'}
		];
		var log, className = "dark";
		
		function onClick(e,treeId, treeNode) {
			$("#goodsType").val(treeNode.name);
			$("#goodsTypeId").val(treeNode.id);
			hideMenu();
		}
		
		function beforeDrag(treeId, treeNodes) {
			return false;
		}
		function beforeEditName(treeId, treeNode) {
		
		}
		
		function beforeRemove(treeId, treeNode) {
			return confirm("确认删除 [" + treeNode.name + "] 吗？");
		}
		
		function onRemove(e, treeId, treeNode) {
		    var id = treeNode.id;
		    $.ajax({
		       type: 'post',
		       url:'deleteGoodsType.action',
		       dataType:'text',
		       data:'goodsTypeId=' + id,
		       cache: false,
		       success: function(result){
		          if("y" != result){
		             alert("系统出现异常，请与程序开发员联系！");
		          }
		       }
		    });
		}
		
		var oldTreeNodeName = "";
		function beforeRename(treeId, treeNode, newName) {
			className = (className === "dark" ? "":"dark");
			oldTreeNodeName = treeNode.name;
			if (newName.length == 0) {
				alert("节点名称不能为空.");
				var zTree = $.fn.zTree.getZTreeObj("goodsTypeTree");
				setTimeout(function(){zTree.editName(treeNode)}, 10);
				return false;
			}
			return true;
		}
		function onRename(e, treeId, treeNode) {
		    var treeNodeName = treeNode.name;
		    if(treeNodeName.indexOf(";") <= 0){
		       treeNode.name = oldTreeNodeName;
		       alert("请输入正确格式的类型名称");
		       return;
		    }
		    var index = treeNodeName.indexOf(";");
		    if(index == (treeNodeName.length-1)){
		       treeNode.name = oldTreeNodeName;
		       alert("请输入正确格式的类型名称");
		       return;
		    }
		    $.ajax({
		       type: "post",
		       url: "reNameGoodsType.action",
		       dataType: "text",
		       contentType: "application/x-www-form-urlencoded; charset=utf-8", 
		       data: "goodsTypeId=" + treeNode.id + "&goodsTypeName=" + treeNodeName,
		       cache: false,
		       success: function(result){
		          if("n" == result){
		             alert("系统发生异常，请与管理员联系！");
		          }
		       }
		    });
		}
		
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}

		var newCount = 1;
		function addHoverDom(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return;
			var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id
				+ "' title='add node' onfocus='this.blur();'></button>";
			sObj.append(addStr);
			var btn = $("#addBtn_"+treeNode.id);
			if (btn) btn.bind("click", function(){
				var zTree = $.fn.zTree.getZTreeObj("goodsTypeTree");
                $.ajax({
                   type: 'post',
                   url: 'addGoodsType.action',
                   dataType: 'text',
                   data: 'pId='+ treeNode.id,
                   cache: false,
                   success: function(result){
                   	  if("n" != result){
                   	     zTree.addNodes(treeNode, {id:result, pId:treeNode.id, name:"新建类型;new type"});
                   	  }else{
                   	     alert("系统出现异常，请与管理员联系");
                   	  }
                   }
                });				
				return false;
			});
		};
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.id).unbind().remove();
		};
		function selectAll() {
			var zTree = $.fn.zTree.getZTreeObj("goodsTypeTree");
			zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
		}
		function showMenu() {
			var cityObj = $("#goodsType");
			var cityOffset = $("#goodsType").offset();
			$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
			$("body").bind("mousedown", onBodyDown);
			
		}
		function hideMenu() {
			$("#menuContent").fadeOut("fast");
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
				hideMenu();
			}
		}
		
		
		$(document).ready(function(){
		    //var goodsType = '${goodsTypeJsonOfBackground}';
		    //var goodsTypeJson = eval(goodsType);
		    //zNodes = goodsTypeJson;
		    //alert(zNodes);
			showMenu();
			$.fn.zTree.init($("#goodsTypeTree"), setting, zNodes);
		});